package application.ratt.data_reader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import android.content.SharedPreferences;
import android.util.Log;
import application.ratt.search_alg.AStarAlg;
import application.ratt.search_alg.RouteNodeInformation;


import application.ratt.city_model.City;
import application.ratt.city_model.Station;
import application.ratt.exceptions.InterruptRequestException;
import application.ratt.search_alg.YenAlg;
import application.ratt.utils.WriteFormattedXml;
	
public class MainClass {
	
	private double currentLocationLatitude = 0;
    private double currentLocationLongitude = 0;
    private double desiredDestinationLatitude = 0;
    private double desiredDestinationLongitude = 0;
    
	private static MainClass instance = null;
	
	   protected MainClass() {
	      // Exists only to defeat instantiation.
		   Log.i("testConstruct", this.toString());
	   }
	   public static MainClass getInstance() {
	      if(instance == null) {
	         instance = new MainClass();
	      }
	      return instance;
	   }
	
	public void calculateRoute(InputStream dbInputStream, File cachedDataFile, SharedPreferences settings, int nrOfRoutes) throws ParserConfigurationException, InterruptRequestException
	{
     	CityReader cr = new CityReader();
     	City city = cr.readCity(dbInputStream);
     	WriteFormattedXml xmlFile = new WriteFormattedXml();
     	boolean isCurrentCoordinatesSet = true;
     	boolean isDesiredCoordinatesSet = true;
     	
     	Station s3 = new Station("0", "start", "start", "start");
     	isCurrentCoordinatesSet = s3.setCoordinates(currentLocationLatitude, currentLocationLongitude);
     	Station s4 = new Station("1", "dest", "dest", "dest");
     	isDesiredCoordinatesSet = s4.setCoordinates(desiredDestinationLatitude, desiredDestinationLongitude);
     	if (isCurrentCoordinatesSet && isDesiredCoordinatesSet){
     		ArrayList<ArrayList<RouteNodeInformation>> paths = new YenAlg().YenSearch(s3, s4, city, nrOfRoutes, settings);
     		
     		xmlFile.createXMLHeader();
     		for (ArrayList<RouteNodeInformation> path : paths){
     			xmlFile.writeRoute(path, city, cachedDataFile, settings);
     		}
     		try {
				xmlFile.writeXMLFile(cachedDataFile);
			} catch (TransformerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
     	}
	}
	
	public void setCoordinates(float currentLocationLatitude, float currentLocationLongitude, float desiredDestinationLatitude, float desiredDestinationLongitude){
		this.currentLocationLatitude = currentLocationLatitude;
		this.currentLocationLongitude = currentLocationLongitude;
		this.desiredDestinationLatitude = desiredDestinationLatitude;
		this.desiredDestinationLongitude = desiredDestinationLongitude;
	}
		
	public synchronized void setInterruptRequest(boolean request){
		AStarAlg.getInstance().setInterruptRequest(request);
	}
}
